package com.zcmob.web.admanagement.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zcmob.web.admanagement.db.AppDao;
import com.zcmob.web.admanagement.util.StringUtils;
import com.zcmob.web.admanagement.vo.AdUser;

/**
 * Servlet implementation class for Servlet: LoginServlet
 * 
 */
public class RegistServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    static final long serialVersionUID = 1L;

    /*
     * (non-Java-doc)
     * 
     * @see javax.servlet.http.HttpServlet#HttpServlet()
     */
    public RegistServlet() {
        super();
    }

    /*
     * (non-Java-doc)
     * 
     * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
     *      HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /*
     * (non-Java-doc)
     * 
     * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
     *      HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");

        String username = request.getParameter("username");
        String password = request.getParameter("pwd");
        String company = request.getParameter("company");
        String location = request.getParameter("location");
        String contact_email = request.getParameter("contact_email");
        String contact_phone = request.getParameter("contact_phone");
        
        if (StringUtils.isNullOrEmpty(username) || StringUtils.isNullOrEmpty(password) ||
                StringUtils.isNullOrEmpty(company) || StringUtils.isNullOrEmpty(location) || 
                StringUtils.isNullOrEmpty(contact_email) || StringUtils.isNullOrEmpty(contact_phone)) {
            // goto error page
            return;
        }
        AdUser aduser = AppDao.getInstance().getAdUserByUsername(username);
        if (aduser != null) {
            // 
            request.setAttribute("validerror", "用户名已经存在");
            RequestDispatcher rd = request.getRequestDispatcher("ad_login.jsp");
            rd.forward(request, response);
            return;
        } else {
        
            AdUser newaduser = new AdUser();
            newaduser.setUsername(username);
            newaduser.setPassword(password);
            newaduser.setCompany(company);
            newaduser.setLocation(location);
            newaduser.setContact_email(contact_email);
            newaduser.setContact_phone(contact_phone);
            
            AppDao.getInstance().insertAd_user(newaduser);
            
            RequestDispatcher rd = request.getRequestDispatcher("ad_login.jsp");
            rd.forward(request, response);
        }
    }
}